$(document).ready(function() {
	$('#contractTable').dataTable({
		"bProcessing" : true, //DataTables载入数据时，是否显示‘进度’提示
        "bServerSide" : true, //是否启动服务器端数据导入
        "bStateSave" : false, //是否打开客户端状态记录功能,此功能在ajax刷新纪录的时候不会将个性化设定回复为初始化状态
        "bJQueryUI" : true, //是否使用 jQury的UI theme
        "aLengthMenu" : [5, 10, 15], //更改显示记录数选项
        "iDisplayLength" : 10, //默认显示的记录数
        "bAutoWidth" : true, //是否自适应宽度 update\
        "bScrollCollapse" : false, //是否开启DataTables的高度自适应，当数据条数不够分页数据条数的时候，插件高度是否随数据条数而改变
        "bPaginate" : true, //是否显示（应用）分页器
        "bInfo" : true, //是否显示页脚信息，DataTables插件左下角显示记录数
        "sPaginationType" : "full_numbers", //详细分页组，可以支持直接跳转到某页
        "bSort" : true, //是否启动各个字段的排序功能
        "aaSorting" : [[8, "ASC"]], //默认的排序方式，第3列，升序排列
        "bFilter" : false, //是否启动过滤、搜索功能
        "sScrollX": "100%",
		"sScrollXInner": 1680,
        "sPaginationType": "full_numbers",
	    "sAjaxSource":"loan/ContractManage/list",
        "fnServerData":retrieveData,  //自定义数据获取函数
        
		"aoColumns" : [{
			"mDataProp" : "loanCode",
			"sTitle" : "贷款申请号",
			"bSortable": true,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
		        return '<a href="javascript:void(0);" onclick="loan_query('+row.loanId+','+row.clientId+');" class="tablelink" >'+data+'</a>';
		     }
		}, {
			"mDataProp" : "contractCode",
			"sTitle" : "合同编号",
			"bSortable": true,
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "loanBorrower",
			"sTitle" : "贷款人",
			"bSortable": true,
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "clientIdCard",
			"sTitle" : "身份证号",
			"bSortable": true,
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "loanLimitApply",
			"sTitle" : "申请额度（元）",
			"bSortable": true,
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "finalLimit",
			"sTitle" : "终审额度（元）",
			"bSortable": true,
			"sDefaultContent" : "",
			"sClass" : "center"
		},{
			"mDataProp" : "finalDeadline",
			"sTitle" : "终审期限（月）",
			"bSortable": true,
			"sDefaultContent" : "",
			"sClass" : "center"		
		},{
			"mDataProp" : "finalRate",
			"sTitle" : "终审利率（%）",
			"bSortable": true,
			"sDefaultContent" : "",
			"sClass" : "center"		
		},{
			"mDataProp" : "contractStatus",
			"sTitle" : "合同状态",
			"bSortable": true,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
		        return parent._local_contract_status[data]||'异常';
		     }
		},{
			"mDataProp" : "id",
			"sTitle" : "操作",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center",
			"sWidth":"15%",
			"mRender": function (data, display, row) {
		      var id = row.id,contractStatus=row.contractStatus,loanId = row.loanId;
		      return parent._set_href(contractStatus,[loanId,row.clientId,id],parent._local_contract_opt);
		    }
			
		}],
		"oLanguage": {
            "sUrl":"static_resources/language/lang-cn.txt" //这个是国际化，也就是汉化的各种文字数据源，写对url就行了
        }
        
	});
	
	//查询事件
	$('input[name="query_btn"]').click(function(){
		$('#contractTable').dataTable().fnDraw();
	});
	
	$('#contractStatus').mSelect({
		//myClass:'',
		width:100,
		baseCode:'Loan_Contract_Status_Code'
		//isAllSelect:false,
		//dataOptions:'required:true'
	});
	
});
var contract_edit = function(id,clientId,contractId){
	parent._do_goto_right('jsp/loan/loan_contract_add_or_edit.jsp?id='+id+'&clientId='+clientId+'&contractId='+contractId);
};

var contract_guarantee = function(id){
	parent._do_goto_right('jsp/loan/loan_contract_guarantee_add_or_edit.jsp?loanId='+id);
};

var contract_query = function(id){
	parent._do_goto_right('jsp/loan/loan_contract_detail.jsp?loanId='+id);
};

//合同打印
var contract_print = function(id,clientId,contractId){
	$.dialog({
		title:"合同打印",
	    width: 600,
	    height: 400,
	    content: 'url:jsp/loan/select_contract_print.jsp?contractId='+contractId+'&loanId='+id+'&clientId='+clientId	    
	});
};


//var contract_print = function(id,clientId,contractId){
	
//	$.dialog.confirm('确定合同打印吗？', function(){
//		window.location.href = 'loan/ContractManage/printContract?id='+contractId+'&loanId='+id+'&clientId='+clientId+'&contractType=1';
//		$.ajax({
//	       	url:'loan/ContractManage/printContract',
//	    	data:{'id':contractId,'loanId':id,'clientId':clientId,'contractType':0},
//	    	type:'post',
//	    	//dataType:'json',
//	    	async:false,
//	    	success:function(result){
//	    	        if(typeof(result)!='undefined'){
//	    	        	if(result.state == 200){
//	    	        		$.dialog.alert('打印成功！');
//	    	        		return false;
//	    	        	}
//	    	        }
//	    	        $.dialog.alert('打印失败！');
//	    	        return false;
//	    		},
//	    		complete:function(){
//	    		}
//	        });
//	});
//}
//合同签订
var contract_sign = function(id,clientId,contractId){
	parent._do_goto_right('jsp/loan/loan_contract_audit.jsp?loanId='+id+'&clientId='+clientId+'&contractId='+contractId);
	//loan_contract_audit.jsp
};

//合同上传
var contract_loading = function(id,clientId){
	$.dialog.confirm('如果合同已经上传，将会被覆盖，确定吗？', function(){
		$.dialog({
			id:'loan_contract_accessory_upload_dailog',
			title:'上传合同附件',
			lock:true,
			width:700,
			height:400,
			content:'url:'+parent.basePath+'jsp/loan/loan_contract_accessory_upload.jsp?loanId='+id+'&clientId='+clientId,
			//data:{'loadId':id,'clientId':clientId},
			close: function(){
				$('#contractTable').dataTable().fnDraw();
		    }
		});
	});
}

//中止订单
var loan_stop = function (id){
	$.dialog.confirm('你确定要作废这条贷款单吗？', function(){
		$.ajax({
	       	url:'sys/LoanExt/stop',
	    	data:{pks:id},
	    	type:'post',
	    	dataType:'json',
	    	success:function(result){
	    	        if(result.state="200"){
	    	        	$.dialog.alert('中止申请单成功！');
	    	        	$('#contractTable').dataTable().fnDraw();
	    	        }else{
	    	        	$.dialog.alert('中止申请单失败！');
	    	        }
	    		}
	        });
	});
};


//自定义数据获取函数
function retrieveData( sSource, aoData, fnCallback ) {
	var array = $("#_form_search_default").serializeArray();
	aoData = parent._set_query_conditions(aoData,array);
        $.ajax( {
                type: "POST", 
                url: sSource, 
                cache : false,
                dataType : "json",
                data : aoData, 
                success: function(resp) {
                	if(resp && resp.state == 200){
                		fnCallback(resp.datagrid);
                		return false;
                	}
                	fnCallback(null);
            }
        });
}